from django.http import HttpResponse
from django.contrib.sites.models import Site
#from Domain import Domain
from httprouter.service.models import Rule
from httprouter.service.models import Target
from urlparse import urlparse

def index(request):
#domain = request.META['HTTP_HOST']
#Ddomain = request.get_host()
	#et_full_path()

	response = HttpResponse()

	path = request.get_full_path()

	#d = Domain.objects.get(name=domain)
	s = Site.objects.get(domain=request.get_host())

	#rules = Rule.objects.get(site=s)

	for r in Rule.objects.filter(site=s):
		if ( path.startswith(r.path) ):
			list = Target.objects.filter(rule=r)
			response.write(r.path)		
			#reproxy = ",".join(list)
			response['X-REPROXY-URL'] = list[0].value


	#path = get_full_path()

#	rules = site.rules.all()	

	

#	response['X-REPROXY-URL'] = 'http://localhost'
#	response['X-Reproxy-Cache-For'] = '3600; Content-Type Last-Modified'

#	response.write(rules)
#	response.write(domain)
#	response.write(request)
	
#	e.authors.all() # Returns all Author objects for this Entry.
#	e.authors.count()
#	e.authors.filter(name__contains='John')

	return response

	#ite.objects.get_current()


	#return HttpResponse(domain)


#request.if not request.META['HTTP_HOST'].endswith('mysite.com.au'):u
	#return HttpResponse(request)
#	return HttpResponse(Site.objects.get_current())

def detail(request, poll_id):
    return HttpResponse("You're looking at poll %s." % poll_id)

def add_route(request, domain):
    o = urlparse(domain)
    print o[0]
    print o[1]
    print o[2]
    print o[3]
    return HttpResponse("Adding route %s " % o[1])
